package com.allen.library.observer;

import android.util.Log;

import com.allen.library.base.LangDialogFragment;
import com.allen.library.exception.ApiException;
import com.allen.library.interfaces.LoginISubscriber;
import com.allen.library.manage.RxHttpManager;

import com.tencent.bugly.crashreport.CrashReport;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import retrofit2.HttpException;


/**
 * Created by Allen on 2017/5/3.
 *
 * @author Allen
 * <p>
 * 基类BaseObserver
 */

public abstract class LoginBaseObserver<T> implements Observer<T>, LoginISubscriber<T> {



    /**
     * 是否隐藏toast
     *
     * @return
     */
    protected boolean isHideToast() {
        return false;
    }

    /**
     * 标记网络请求的tag
     * tag下的一组或一个请求，用来处理一个页面的所以请求或者某个请求
     * 设置一个tag就行就可以取消当前页面所有请求或者某个请求了
     * @return string
     */
    protected String setTag(){
        return null;
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        RxHttpManager.get().add(setTag(), d);
        doOnSubscribe(d);
    }

    @Override
    public void onNext(@NonNull T t) {

        doOnNext(t);
    }

    @Override
    public void onError(@NonNull Throwable e) {


        String error = ApiException.handleException(e).getCode()+"";
//       if (error.equals("303")){
//            String string=null;
//            ResponseBody aa = ((HttpException) e).response().errorBody();
//            try {
//                if (aa!=null){
//                    string = aa.string();
//                }
//
//            } catch (IOException ex) {
//                ex.printStackTrace();
//            }
//            setError(error,string);
//        }else{
//            setError(error,"");
//        }
        setError(error,"");
        CrashReport.postCatchedException(ApiException.handleException(e));



//        ToastUtils.showToast(ApiException.handleException(e).getCode()+"");

    }


    @Override
    public void onComplete() {
        doOnCompleted();
    }


    private void setError(String errorMsg,String errorBody) {
        doOnError(errorMsg,errorBody);
    }
    public static String getResponseBody(ResponseBody responseBody) {

        Charset UTF8 = Charset.forName("UTF-8");

        BufferedSource source = responseBody.source();
        try {
            source.request(Long.MAX_VALUE); // Buffer the entire body.
        } catch (IOException e) {
            e.printStackTrace();
        }
        Buffer buffer = source.buffer();

        Charset charset = UTF8;
        MediaType contentType = responseBody.contentType();
        if (contentType != null) {
            try {
                charset = contentType.charset(UTF8);
            } catch (UnsupportedCharsetException e) {
                e.printStackTrace();
            }
        }
        return buffer.clone().readString(charset);
    }
}
